package Function;

import java.io.File;
import java.io.Serializable;
import java.util.Vector;

import FCRendering.FCRMaster;
import Function.Utils.FCFileCopyToMaterial;
import VWorldGameStudio.FCTank;
import lombok.Getter;
import utils.FCDataSingles;
import utils.FCFileUrl;

public class FCTankLibrary implements Serializable {
    private Vector<FCTank> tankList = new Vector<FCTank>();

    @Getter
    private Vector<String> tankName = new Vector<String>();
    private FCFileCopyToMaterial fileCopy = new FCFileCopyToMaterial();
    private static final String TANK_LIST_PATH = "./data/tanklist.vw";
    private static final String TANKS_PATH = "./data/tank";
    private int tankallnum = 0;

    /**
     * 初始化坦克类，读取数量
     * 建议游戏启动方使用
     *
     * @date 2021-11-30
     */
    public FCTankLibrary() {
        try {
            this.tankallnum = new File(TANKS_PATH).listFiles().length;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 坦克数据加载函数
     *
     * @date 2021-11-30
     */
    public void loadTankData() {
        try {
            File[] f = new File(TANKS_PATH).listFiles();
            for (int i = 0; i < f.length; i++) {
                String path = f[i].getAbsolutePath();
                if (path.endsWith(".vw")) {
                    FCTank tank = FCDataSingles.getReader().readTankData(path);
                    tankName.add(tank.getID());
                    tankList.add(tank);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取坦克实际数据量
     *
     * @return integer
     * @date 2021-11-30
     */
    public int getTankAllNum() {
        return this.tankallnum;
    }

    /**
     * 读取坦克数据
     *
     * @param isGame
     */
    public FCTankLibrary(boolean isGame) {
        try {
            File[] tanks = new File(TANKS_PATH).listFiles();
            for (File file : tanks) {
                String path = file.getAbsolutePath();
                if (path.endsWith(".vw")) {
                    FCTank tank = FCDataSingles.getReader().readTankData(file.getAbsolutePath());
                    tankName.add(tank.getID());
                    tankList.add(tank);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 注册坦克
    public void registerTanks(boolean isnew, String name, String gunID, String gun2ID, String SEID, String engineID,
                              String CdeviceID, String chassisID, String radarID, String masterPngUrl, String actionPngUrl,
                              String whitelinepngurl, int editIndex) {
        FCTank t = new FCTank();
        t.registerTank(name, gunID, gun2ID, SEID, engineID, CdeviceID, chassisID, radarID,
                fileCopy.copyPngToMaterialPng(2, masterPngUrl), fileCopy.copyPngToMaterialPng(2, actionPngUrl),
                fileCopy.copyPngToMaterialPng(2, whitelinepngurl));
        if (!isnew) {
            // 旧的移除后添加，新的直接添加
            try {
                String oldname = tankName.get(editIndex);
                tankList.remove(editIndex);
                tankName.remove(editIndex);
                File del = new File(FCFileUrl.URL_DATA_TANK + oldname + ".vw");
                del.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        tankName.add(name);
        tankList.add(t);
        FCDataSingles.getWriter().writeTankData(t);
    }

    // 获取最新坦克ID
    public int getNewTankID() {
        return tankName.size();
    }

    // 根据ID获取坦克名称
    public String getTankNameOfID(int ID) {
        return tankName.get(ID);
    }

    // 检测是否存在
    public boolean isTankRepeat(String name) {
        for (int i = 0; i < tankName.size(); i++)
            if (tankName.get(i).equals(name))
                return true;
        return false;
    }

    public FCTank getTankOfIDForPaint(String name, String hostId) {
        if (name == null) return null;
        if (name.equals("null")) return null;
        if (hostId.equals(FCRMaster.gameSave.HOST_UID)) {
            FCTank find = FCRMaster.gameSave.findTankSaveIos(name);
            if(find != null){
                return find;
            }
            int a = tankName.indexOf(name);
            if (a > -1) {
                return tankList.get(a);
            }
            return null;
        } else {
            return FCMutilPlayTankCache.findTankByName(hostId, name);
        }
    }

    // 获取坦克对象根据id
    public FCTank getTankOfID(String name, String hostId) {
        if (name == null) return null;
        if (name.equals("null")) return null;
        if (hostId.equals(FCRMaster.gameSave.HOST_UID)) {
            FCTank find = FCRMaster.gameSave.findTankSaveIos(name);
            if(find != null){
                return find;
            }
            int a = tankName.indexOf(name);
            if (a > -1) {
                System.out.println("什么，需要克隆新车？  "+name);
               return tankList.get(a).getClone();
            }
            return null;
        } else {
            return FCMutilPlayTankCache.findTankByName(hostId, name);
        }
    }

    public FCTank getTankOfNum(int num) {
        return tankList.get(num);
    }

}
